package com.cgq.base.dao.impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.cgq.base.dao.BaseDao;
import com.cgq.utils.ReflectUtil;

@SuppressWarnings("unchecked")
@Component
public class BaseDaoImpl<T> 
			extends HibernateDaoSupport implements BaseDao<T> {

	protected HibernateTemplate hibernateTemplate=null;
	
	private Class<T> entityClass=ReflectUtil.getEntity(this.getClass());

	@Autowired
	public void setSessionFactoryUtil(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
		hibernateTemplate=this.getHibernateTemplate();
	}
	
	public T save(T entity) {
		return (T) hibernateTemplate.save(entity);
	}

	/**修改数据  */
	public void update(T entity) {
		hibernateTemplate.update(entity);
	}

	/**删除数据*/
	public void delete(String[] ids) {
		for (String id : ids) {
			hibernateTemplate.delete(find(id));
		}
	}

	/**查找数据 */
	public T find(String id) {
		return hibernateTemplate.get(entityClass, id);
	}
}
